package com.fa4j.product.category.repository.impl;

import com.fa4j.common.data.repository.impl.BaseRepositoryImpl;
import com.fa4j.product.category.model.Category;
import com.fa4j.product.category.repository.CategoryAttributeRepository;
import com.fa4j.product.category.repository.CategoryRepository;
import com.fa4j.product.category.repository.mapper.CategoryMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

/**
 * 持久化接口实现-商品分类
 *
 * @since 2025-11-22 21:06:24
 */
@Repository
@RequiredArgsConstructor
public class CategoryRepositoryImpl extends BaseRepositoryImpl<CategoryMapper, Category> implements CategoryRepository {

    private final CategoryAttributeRepository categoryAttributeRepository;

    @Override
    public List<Category> queryAllList() {
        return super.list();
    }

    @Override
    public long countByParentId(Integer parentId) {
        return super.lambdaQuery().eq(Category::getParentId, parentId).count();
    }

    @Override
    public Optional<Category> findById(Serializable id) {
        return super.findById(id)
                .map(category -> {
                    category.setAttrList(categoryAttributeRepository.queryByCategoryId((Integer) id));
                    return category;
                });
    }
}


